<template>
  <div style="padding-top: 30px">
    <div class="menuOperation">
      <el-button type="primary" @click="addMenu">新增</el-button>
    </div>
    <div class="menuList">
      <ul class="menuTitle">
        <li>
          <span>名称</span>
        </li>
        <li>
          <span>URL</span>
        </li>
        <li>
          <span>图标</span>
        </li>
        <li>
          <span>是否启用</span>
        </li>
        <li>
          <span>功能按钮</span>
        </li>
        <li>
          <span>描述</span>
        </li>
        <li>
          <span>操作</span>
        </li>
      </ul>
      <ul class="menuBody">
        <li class="menuitem currentParentMenu" v-for="item in menuLists" :key="item.id" style="border-bottom: 1px solid #ebeef5">
          <div style="width: 100%;" class="line">
            <div>
              <i
                class="el-icon-circle-plus-outline"
                style="margin-right: 5px;"
                @click="unfold($event)"
                v-if="item.children.length!=0"
              ></i>
              <span>{{item.menuName}}</span>
            </div>
            <div>
              <span>{{item.menuUrl}}</span>
            </div>
            <div>
              <i class="fa fa-home"></i>
            </div>
            <div>
              <span>{{item.menuEnable===0?'不启用':'启用'}}</span>
            </div>
            <div>
              <span></span>
            </div>
            <div>
              <span class="menuDesc" title="item.menuDesc">{{item.menuDesc}}</span>
            </div>
            <div>
              <!-- <a href="javascript:;" @click='addSubMenu(item)'>新增子菜单</a> -->
              <a href="javascript:;" @click="updateMenu(item)">编辑</a>
              <a href="javascript:;" @click="deleteMenu(item)">删除</a>
            </div>
          </div>
          <ol class="subMenu">
            <li class="subItem" v-for="son in item.children" :key="son.id">
              <div>
                <i class="fa fa-plus" style="margin-right: 5px;" v-show="hasSonMenu"></i>
                <span>{{son.menuName}}</span>
              </div>
              <div>
                <span>{{son.menuUrl}}</span>
              </div>
              <div>
                <i class="fa fa-home"></i>
              </div>
              <div>
                <span>{{son.menuEnable===0?'不启用':'启用'}}</span>
              </div>
              <div>
                <el-tag size="mini" v-for="tag in son.children" :key="tag.id" :type="'success'">{{tag.menuName}}</el-tag>
              </div>
              <div>
                <span title="son.menuDesc" class="menuDesc">{{son.menuDesc}}</span>
              </div>
              <div>
                <!-- <a href="javascript:;">新增子菜单</a> -->
                <a href="javascript:;" @click="updateSonMenu(item,son)">编辑</a>
                <a href="javascript:;" @click="deleteMenu(son)">删除</a>
              </div>
            </li>
          </ol>
        </li>
      </ul>
    </div>
    <!-- 新增 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="类型" prop="types">
          <el-radio-group v-model="ruleForm.types" @change="getCurrentType">
            <el-radio label="0">目录</el-radio>
            <el-radio label="1">菜单</el-radio>
            <el-radio label="2" v-if="!showBtns">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="menuName" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="父级名称" prop="fatherName" v-if="parentName">
          <el-cascader :options="options" :show-all-levels="false" @change="getFatherId" v-model="selectedOptions"></el-cascader>
        </el-form-item>
        <el-form-item label="URL" prop="urls" v-if="parentName&&isBtns">
          <el-input v-model="ruleForm.urls"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="isUse">
          <el-radio-group v-model="ruleForm.isUse">
            <el-radio label="启用"></el-radio>
            <el-radio label="不启用"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图标" prop="icons">
          <input type="file" name>
        </el-form-item>
        <el-form-item label='所属平台'>
          <el-checkbox-group v-model="ruleForm.menuTypes">
            <el-checkbox label="PC" name="menuTypes"></el-checkbox>
            <el-checkbox label="APP" name="menuTypes"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="按钮组" v-if="showBtns">
          <template>
            <el-tag v-for="btn in existBtns" :key="btn.id">{{btn.menuName}}</el-tag>
          </template>
        </el-form-item>
        <el-form-item label="描述信息" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "MenuManage",
  inject: ["reload"],
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        name: "",
        icons: "",
        isUse: "",
        desc: "",
        urls: "",
        fatherName: "",
        parentId: "",
        types: null,
        menuTypes: []
      },
      value4: "",
      options: [],
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        types: [{
          required: true,
          message: "请选择新增类型",
          trigsger: "change"
        }],
        icons: [{
          required: false,
          message: "请上传图标",
          trigger: "change"
        }],
        isUse: [
          { required: true, message: "请勾选是否启用", trigger: "change" }
        ],
        // desc: [
        // { required: true, message: '请填写描述信息', trigger: 'blur' }
        // ],

        urls: [{ required: true, message: "请填写跳转Url", trigger: "blur" }]
      },
      menuLists: [],
      dialogTitle: "新增",
      menuName: "名称",
      fileList: [],
      hasSonMenu: false,
      isAddSubMenu: false,
      parentName: true,
      isUpdate: false,
      secondMenu: false,
      isBtns: true,
      selectedOptions: [],
      showBtns: false,
      existBtns: []
    };
  },
  created() {
    this.initMenuLists();
  },
  methods: {
    initMenuLists() {
      this.$common.$ajax("/sys/menu/list", {}, "get").then(res => {
        this.menuLists = res.data.body;
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        // alert(this.parentName, this.isUpdate)
        if (valid) {
          if (this.parentName && !this.isUpdate) {
            //新增子菜单

            this.$common
              .$ajax(
                "/sys/menu/save", {
                  menuName: this.ruleForm.name,
                  menuUrl: this.ruleForm.urls,
                  menuEnable: this.ruleForm.isUse === "不启用" ? 0 : 1,
                  menuDesc: this.ruleForm.desc,
                  menuParentId: this.ruleForm.parentId,
                  type: this.ruleForm.types
                },
                "post"
              )
              .then(res => {
                this.$message({
                  type: "success",
                  message: "新增成功!",
                  duration: 1000
                });
                this.dialogVisible = false;
                this.reload();
              });
          } else if (!this.parentName && !this.isUpdate) {
            //新增目录

            this.$common
              .$ajax(
                "/sys/menu/save", {
                  menuName: this.ruleForm.name,
                  menuEnable: this.ruleForm.isUse === "不启用" ? 0 : 1,
                  menuDesc: this.ruleForm.desc,
                  type: this.ruleForm.types
                },
                "post"
              )
              .then(res => {
                this.$message({
                  type: "success",
                  message: "新增成功!",
                  duration: 1000
                });
                this.dialogVisible = false;
                this.reload();
              });
          } else if (!this.parentName && this.isUpdate) {
            //更新目录

            this.$common
              .$ajax(
                "/sys/menu/update", {
                  id: sessionStorage.currentUpdateMenuId,
                  menuName: this.ruleForm.name,
                  menuEnable: this.ruleForm.isUse === "不启用" ? 0 : 1,
                  menuDesc: this.ruleForm.desc
                },
                "post"
              )
              .then(res => {
                this.$message({
                  type: "success",
                  message: "更新成功！"
                });
                this.dialogVisible = false;
                this.reload();
                // this.$router.push('/main/MenuManage')
              });
          } else if (this.parentName && this.isUpdate) {
            //更新子目录

            this.$common
              .$ajax(
                "/sys/menu/update", {
                  id: sessionStorage.currentUpdateMenuId,
                  menuName: this.ruleForm.name,
                  menuEnable: this.ruleForm.isUse === "不启用" ? 0 : 1,
                  menuDesc: this.ruleForm.desc,
                  menuParentId: this.ruleForm.parentId,
                  menuUrl: this.ruleForm.urls
                },
                "post"
              )
              .then(res => {
                this.$message({
                  type: "success",
                  message: "更新成功！"
                });
                this.dialogVisible = false;
                this.reload();
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      console.log(formName);
      this.$refs[formName].resetFields();
      for (let i in this.ruleForm) {
        this.ruleForm[i] = "";
      }
    },

    unfold(e) {
      var isUnfold = false;

      // console.log(e.currentTarget)
      // 获取当前的子列表
      var child_node =
        e.currentTarget.parentElement.parentElement.nextElementSibling;
      if (e.currentTarget.classList.contains("el-icon-circle-plus-outline")) {
        child_node.style.display = "block";
        e.currentTarget.classList.add("el-icon-remove-outline");
        e.currentTarget.classList.remove("el-icon-circle-plus-outline");
      } else {
        child_node.style.display = "none";
        e.currentTarget.classList.add("el-icon-circle-plus-outline");
        e.currentTarget.classList.remove("el-icon-remove-outline");
      }
    },
    addMenu() {
      //增加目录
      this.dialogVisible = true;
      this.dialogTitle = "新增";
      this.showBtns = false;
    },
    updateMenu(item) {
      //修改目录
      console.log(item);
      this.isUpdate = true;
      this.dialogVisible = true;
      this.dialogTitle = "修改";
      this.parentName = false;
      this.ruleForm.name = item.menuName;
      this.ruleForm.isUse = item.menuEnable === 1 ? "启用" : "不启用";
      this.ruleForm.desc = item.menuDesc;
      this.ruleForm.types = item.type.toString();
      sessionStorage.currentUpdateMenuId = item.id;
    },
    addSubMenu(item) {
      //增加子菜单
      for (let i in this.ruleForm) {
        if (this.ruleForm[i] != "") {
          this.ruleForm[i] = "";
        }
      }
      this.isUpdate = false;
      this.dialogVisible = true;
      this.isAddSubMenu = true;
      this.dialogTitle = "新增";
      this.parentName = true;
      this.ruleForm.fatherName = item.menuName;

      sessionStorage.currentParentId = item.id;
    },
    deleteMenu(item) {
      sessionStorage.currentDeleteMenuId = item.id;
      this.$confirm("此操作将永久删除该选项, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          this.$common
            .$ajax(
              "/sys/menu/delete", { menuId: sessionStorage.currentDeleteMenuId },
              "get"
            )
            .then(res => {
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.reload();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    updateSonMenu(name, son) {
      console.log(son);
      this.ruleForm.fatherName = name.menuName;
      this.isUpdate = true;
      this.dialogVisible = true;
      this.dialogTitle = "修改";
      this.parentName = true;
      this.ruleForm.name = son.menuName;
      this.ruleForm.isUse = son.menuEnable === 1 ? "启用" : "不启用";
      this.ruleForm.desc = son.menuDesc;
      this.ruleForm.urls = son.menuUrl;
      this.ruleForm.parentId = name.id;
      this.ruleForm.types = son.type.toString();
      sessionStorage.currentUpdateMenuId = son.id;
      this.options = this.$store.state.navs;
      this.options.forEach(item => {
        delete item.children;
      });
      this.selectedOptions = [name.id];
      this.showBtns = true;
      this.existBtns = son.children;
    },
    getCurrentType(val) {
      console.log(val);

      if (val == 0) {
        this.parentName = false;
        this.secondMenu = false;
        this.showBtns = false;
      } else if (val == 1) {
        this.secondMenu = true;
        this.options = this.$store.state.navs;
        this.options.forEach(item => {
          delete item.children;
        });
        this.isBtns = true;
        this.parentName = true;
        this.secondMenu = true;
      } else {
        this.secondMenu = false;
        this.options = this.$store.state.navs;
        this.isBtns = false;
      }
    },
    getFatherId(val) {
      console.log(val);
      if (this.secondMenu) {
        this.ruleForm.parentId = val[0];
        this.ruleForm.types = "1";
      } else {
        this.ruleForm.parentId = val[1];
        this.ruleForm.types = "2";
      }
    }
  }
};

</script>
<style lang="scss" scoped>
.menuList {
  width: 100%;
  margin-top: 30px;

  .menuTitle {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #ebeef5;

    li {
      height: 40px;
      // width: 18%;
      width: calc(100% / 7);

      span {
        font-size: 16px;
        display: inline-block;
        height: 100%;
        line-height: 40px;
        width: 100%;

        text-align: left;
      }

      // &:nth-child(3) {
      // width: 10%
      // }
    }
  }

  .menuBody {
    border-bottom: 1px solid #ebeef5;
    width: 100%;

    li.menuitem {
      width: 100%;

      div {
        height: 50px;
        display: flex;
        width: calc(100% / 7);
        align-items: center;
        font-size: 14px;

        // &:nth-child(3) {
        //   width: 10%
        // }

        a {
          margin-right: 15px;
          color: #409eff;

          &:hover {
            cursor: pointer;
            color: #1268c1;
          }
        }
      }
    }
  }

  .subMenu {
    width: 100%;
    min-height: 50px;
    display: none;
    padding-left: 20px;

    .subItem {
      width: 100%;
      height: 50px;

      div {
        float: left;
      }
    }
  }
}

.line .menuDesc {
  display: inline-block;
  width: 90%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

</style>
